﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AvatarHelper;
using System.Web.Security;
using Avatar.Model;
using Avatar.Common;

public partial class Company_ADManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                if (Membership.GetUser() != null)
                {
                    gv_AD.DataSource = _adc.AD.Where(a => a.AdsBy == Membership.GetUser().UserName);
                    gv_AD.DataBind();

                    var _balabce = _adc.CompanyFinancialAccount.Where(b => b.Company == Membership.GetUser().UserName).FirstOrDefault();
                    if (_balabce == null) lbl_Balance.Text = "0.00";
                    else
                        lbl_Balance.Text = _balabce.Balance.ToString();
                }
            }
        }
    }
    protected ADPrice GetPrice(Guid _id)
    {
        using (var _adc = DBHelper.GetDataContext())
        {
            var _ad= _adc.AD.Where(a=>a.ID==_id).FirstOrDefault();
            if (_ad == null) throw new Exception("广告不存在");

            var _adPrice = _ad.ADCategory.ADPrice.Where(p => p.PeriodID == _ad.PeriodID).FirstOrDefault();
            if (_adPrice == null) throw new Exception("对应价格不存在");

            return _adPrice;  
        }
    }
    protected void btn_Pay_Click(object sender, EventArgs e)
    {
        try
        {
            var _btn = sender as Button;
            var _id = string.IsNullOrEmpty(_btn.CommandArgument) ? Guid.Empty : new Guid(_btn.CommandArgument);
            using (var _adc = DBHelper.GetDataContext())
            {
                var _ad = _adc.AD.Where(a => a.ID == _id).FirstOrDefault();
                if (_ad == null) throw new Exception("广告不存在");

                var _adPrice = _ad.ADCategory.ADPrice.Where(p => p.PeriodID == _ad.PeriodID).FirstOrDefault();
                if (_adPrice == null) throw new Exception("对应价格不存在");

                var _balance = _adc.CompanyFinancialAccount.Where(a => a.Company == _ad.AdsBy).FirstOrDefault();
                if (_balance == null) throw new Exception("账户余额不足");

                if (_balance.Balance < _adPrice.Price) throw new Exception("账户余额不足");

                _balance.Balance -= _adPrice.Price;
                var _log = new CompanyFinancialAccountLog();
                _log.AccountCategoryID = 3;
                _log.Date = DateTime.Now;
                _log.Money = _adPrice.Price;
                _log.Company = _ad.AdsBy;
                _adc.CompanyFinancialAccountLog.InsertOnSubmit(_log);

                _ad.Open = true;
                _ad.StartDatetime = DateTime.Now;
                _ad.OverDatetime = DateTime.Now.AddMonths(_adPrice.Period.Month);

                _adc.SubmitChanges();

                Helper.ShowMassage("付款成功！", Request.Url.ToString());
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "付款出错："+_e.Message;
        }
    }
}
